﻿using PlayerRoles;
using PluginAPI.Core;
using PluginAPI.Core.Attributes;
using PluginAPI.Enums;
using PluginAPI.Events;
using System.Text;
using Utils;


namespace Event.Rule
{
    /// <summary>
    /// 
    /// </summary>
    public class WarheadControlEvent
    {
        [PluginEvent(ServerEventType.WarheadStart)]
        public void OnWarheadStart(WarheadStartEvent e)
        {
            TimeCoroutine.Delay(0.5f, () =>
            {
                string player = e.Player.Nickname;
                TeamUtils.GetTeamInfo(e.Player.Team, out string teamInfo);

                string message = $"<color=red>警告</color> | <color=red>ALPHA</color> 核弹已进入引爆程序 | {teamInfo} | {player}";
                MessageStringUtils.GenerateAdminMessage(message, out string output);

                Server.SendBroadcast(output, 10, Broadcast.BroadcastFlags.Truncated, true);
            });
        }

        [PluginEvent(ServerEventType.WarheadStop)]
        public void OnWarheadStop(WarheadStopEvent e)
        {
            TimeCoroutine.Delay(0.5f, () =>
            {
                string player = e.Player.Nickname;

                TeamUtils.GetTeamInfo(e.Player.Team, out string teamInfo);

                string message = $"<color=red>警告</color> | <color=red>ALPHA</color> 核弹暂进入待机状态 | {teamInfo} | {player}";
                MessageStringUtils.GenerateAdminMessage(message, out string output);
                Server.SendBroadcast(output, 10, Broadcast.BroadcastFlags.Truncated, true);
            });
        }

        [PluginEvent(ServerEventType.WarheadDetonation)]
        public void OnWarheadDetonation(WarheadDetonationEvent e)
        {
            SendRoundResult();
        }


        [PluginEvent(ServerEventType.PlayerDying)]
        public void OnSCPAllDead(PlayerDyingEvent ev)
        {
            if (!ev.Player.IsSCP) return;

            TeamUtils.GetTeamMembers(Team.SCPs, out int count);

            if (count != 0) return;

            SendRoundResult();
        }




        /// <summary>
        /// 发送核弹引爆结果
        /// </summary>
        private void SendRoundResult()
        {
            TeamUtils.GetTeamMembers(Team.SCPs, out int teamSCP);
            TeamUtils.GetTeamMembers(Team.FoundationForces, out int teamFoundation);
            TeamUtils.GetTeamMembers(Team.Scientists, out int teamScientist);
            TeamUtils.GetTeamMembers(Team.ChaosInsurgency, out int teamChaos);
            TeamUtils.GetTeamMembers(Team.ClassD, out int teamClassD);
            TeamUtils.GetTeamMembers(Team.Dead, out int teamDeaths);
            TeamUtils.GetTeamMembers(Team.OtherAlive, out int teamOthers);

            TeamUtils.GetTeamInfo(Team.FoundationForces, out string ntfs);
            TeamUtils.GetTeamInfo(Team.Scientists, out string scis);
            TeamUtils.GetTeamInfo(Team.SCPs, out string scps);
            TeamUtils.GetTeamInfo(Team.ClassD, out string clds);
            TeamUtils.GetTeamInfo(Team.ChaosInsurgency, out string chaos);
            TeamUtils.GetTeamInfo(Team.Dead, out string deaths);
            TeamUtils.GetTeamInfo(Team.OtherAlive, out string others);

            StringBuilder build = new StringBuilder();
            build.Append("清算播报");

            if (teamSCP != 0) build.Append($"| [{scps}] x{teamSCP}");
            if (teamFoundation != 0) build.Append($"| [{ntfs}] x{teamFoundation}");
            if (teamScientist != 0) build.Append($"| [{scis}] x{teamScientist}");
            if (teamChaos != 0) build.Append($"| [{chaos}] x{teamChaos}");
            if (teamClassD != 0) build.Append($"| [{clds}] x{teamClassD}");
            if (teamDeaths != 0) build.Append($"| [{deaths}] x{teamDeaths}");
            if (teamOthers != 0) build.Append($"| [{others}] x{teamOthers}");

            Cassie.Message(build.ToString(), false, false, true);
        }
    }
}
